// #include <iostream>
// #include <string>
// #include "compile.hpp"
// #include "runner.hpp"
// using namespace ns_compiler;
// using namespace ns_runner;

#include "compile_run.hpp"

using namespace ns_compile_and_run;
using namespace httplib;

//类，函数的名字大写

void Usage(std::string proc)
{
    std::cerr<<"Useage :"<<"\n\t"<<proc<<"port"<<std::endl;
}

int main(int argc,char*argv[], char *env[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        return 1;
    }

    httplib::Server srv;

    // srv.Get("/hello",[](const Request& req,Response&resp)
    // {
    //     resp.set_content("hello httplib,您好 httplib","text/plain;charset=utf-8");
    // });
    
    srv.Post("/compile_and_run",[env](const Request& req,Response& resp){
        std::string in_json=req.body;
        std::string out_json;

        if(!in_json.empty())
        {
            CompileAndRun::Start(in_json,&out_json, env);
            resp.set_content(out_json,"application/json; charset=utf-8");
        }
    });

    srv.listen("0.0.0.0",atoi(argv[1]));
    return 0;
}


















//     Json::Value in_value;
//     in_value["code"] = R"(#include<iostream>
// int main(){
// std::cout << "你可以看⻅我了" << std::endl;
// return 0;
// })";

//     in_value["input"]="";
//     in_value["cpu_limit"]=1;
//     in_value["mem_limit"]=10240*3;

//     Json::StreamWriterBuilder builder;
//     builder["emitUTF8"] = true;
    
//     std::string in_json;
//     in_json=Json::writeString(builder,in_value);
//     std::cout<<in_json<<std::endl;


//     std::string out_json;
//     CompileAndRun::Start(in_json,&out_json);

//     std::cout<<out_json<<std::endl;